#coding=utf-8
import decimal,sqlite3,urllib,datetime
from BeautifulSoup import BeautifulSoup

indexdatadir = '/home/jay/py/index'
index_infos = [['szzs', '上证指数', '1A0001'],
       ['szcz', '深圳成指', '399001']]

def todec(f, place):

    q = decimal.Decimal( (0, (1, ), -place) )
    r = decimal.Decimal(str(f)).quantize(q)
    return r

def data2csv(data, head, fn):
    
    f = open(fn, 'w')
    
    h = ','.join(head)
    h = h + '\n'
    f.write(h)
    
    i = 1
    for d in data:
        d = [str(j) for j in d]
        s = ','.join(d)
        s = s + '\n'
        f.write(s)
        
        if i%10 == 0:f.flush()
        i = i + 1
    
    f.close()

def csv2data(fn):
    data = []
    
    f = open(fn)
    
    lines = f.readlines()
    lines = lines[1:]
    
    for line in lines:
        line = line[:-1]
        d = line.split(',')
        
        for j in range(len(d)):
            if d[j] == "None":
                d[j] = None
        data.append(d)
    
    return data

def get_soup(url, coding='gb18030'):
    import socket
    socket.setdefaulttimeout(10)
    while True:
        try:
            f = urllib.urlopen(url)
            htmltext = f.read()
            htmltext = htmltext.decode(coding, 'ignore')
            s = BeautifulSoup(htmltext)
            if s:
                return s
        except (socket.timeout,IOError), e:
            print url, e
            time.sleep(1)

class ExdayGenerator(object):
    def __init__(self, start):
        pass
        '''
        self.start = datetime.date( map( int, start.split(-) ) )
        self.current = self.start
        '''
    def next(self):
        pass
        
